<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.dingxiaoyu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 
// +----------------------------------------------------------------------

namespace Common\Model;
use Think\Model;

/**
 * 用户模型
 * @author 
 */

class UserdataModel extends Model {

    protected $_validate = array(
        array('identity', '18', '身份证长度错误', self::EXISTS_VALIDATE, 'length'),
        array('xuehao', '1,2', '学号长度错误', self::EXISTS_VALIDATE, 'length'),
        array('identity', '18', '身份证不唯一', self::EXISTS_VALIDATE, 'unique'),
        array('xuehao', '1,20', '学号被占用', self::EXISTS_VALIDATE, 'unique'), //用户名被占用
    );

    protected $_auto = array (
            array('status','1'),  // 新增的时候把status字段设置为1
            //array('login','0'),  // 新增的时候把login字段设置为1
            // array('password','password_sha1',1,'function') , // 对password字段在新增和编辑的时候使md5函数处理          
            // array('update_time','time',2,'function'), // 对update_time字段在更新的时候写入当前时间戳
            // array('reg_time','time',1,'function'), // 对update_time字段在更新的时候写入当前时间戳
            // array('reg_ip','getIp',1,'callback'), // 对update_time字段在更新的时候写入当前时间戳
    );

    public function lists($status = 1, $order = 'uid DESC', $field = true){
        $map = array('status' => $status);
        return $this->field($field)->where($map)->order($order)->select();
    }
	
	public function editUser($uid = 0,$nickname = '',$email = '',$password = '',$qq = '',$moblie= '',$description = ''){
		if($uid == 0){
			return -2;
		}
		$data = array('uid'=> $uid);
		if(!empty($nickname))$data['nickname'] = $nickname;
		//if(!empty($password))$data['password'] = $password;
		//if(!empty($email))$data['email'] = $email;
		if(!empty($qq))$data['qq'] = $qq;
		if(!empty($description))$data['description'] = $description;
		if(!empty($moblie))$data['mobile'] = $moblie;
		
		if($this->create($data)){
    		$re = $this->save(); 
    		
    	}else{
    		$re = -1;
    	}
    	return $re;
	}

    public function addUser($username,$email,$password){
    	$data = array(
    		"username" => $username,
    		"nickname" => $username,
    		"email" => $email,
    		"password" => $password,
    		);
    	if($this->create($data)){
    		$uid = $this->add(); 
    		if($uid > 0){
    			//添加用户组
    			M('AuthGroupAccess')->add(array('group_id'=> '2','uid' => $uid));
    		}
    	}else{
    		$uid = -1;
    	}
    	return $uid;
    }

    /**
     * 登录指定用户
     * @param  integer $username 登陆用户名
     * @param  integer $password 登陆密码
     * @return boolean      ture-登录成功，false-登录失败
     */
    public function login($xuehao , $card){
    	
        /* 检测是否在当前应用注册 */
        //dump($password);
        $user = $this->where(array('xuehao'=>$xuehao , 'identity'=>$card))->find();

        if(!$user) {
            $this->error = '4012:账号密码错误或用户不存在！'; //应用级别禁用
            return false;
        }elseif (1 != $user['status']) {
            $this->error = '4013:用户被禁用！'; //应用级别禁用
            return false;
        }

        //记录行为
        //action_log('user_login', 'member', $uid, $uid);

        /* 登录用户 */

        $this->autoLogin($user);
        return true;
    }

    /**
     * 注销当前用户
     * @return void
     */
    public function logout(){
        session('user_auth', null);
        session('user_auth_sign', null);
    }

    /**
     * 自动登录用户
     * @param  integer $user 用户信息数组
     */
    private function autoLogin($user){
        /* 更新登录信息 
        $data = array(
            'uid'             => $user['uid'],
            'login'           => array('exp', '`login`+1'),
            'last_login_time' => NOW_TIME,
            'last_login_ip'   => get_client_ip(1),
        );
        //dump($user);
        $this->save($data);
        */
        /* 记录登录SESSION和COOKIES 
        $auth = array(
            'uid'             => $user['uid'],
            'username'        => $user['nickname'],
            'last_login_time' => $user['last_login_time'],
        );
        */
        session('xuehao', $user['xuehao']);
        session('card', $user['identity']);
        session('user_auth', $auth);
        session('user_auth_sign', data_auth_sign($auth));
        //dump(session());
    }

    public function getNickName($xuehao){
        return $this->where(array('xuehao'=>(int)$xuehao))->getField('name');
    }
	
	public function getIp(){
		return ip2long(get_client_ip());
	}

}
